<?php

/* ----------------------------------------------
  Caloris: Install

  $Id$
  $Author$

  Instalace: Steps 1-2
  ---------------------------------------------- */

require_once('../../../caloris.php');
require_once('../../../extensions.php');

//  Settings (from ini file)
loadConstants('../../../cal-components/data/settings.xml');
loadConstants('../../../cal-components/data/modules.xml');

$step = $_REQUEST["step"];

if ($step == '') {
    $step = 1;
}

if ($step == 2) {

    if ($_POST["agree"] != 1) {
        header("Location: install.php");
        exit();
    }

    if ($_SERVER["DOCUMENT_ROOT"] != '') {
        $additional = str_replace($_SERVER["DOCUMENT_ROOT"], '', substr($_SERVER["SCRIPT_FILENAME"], 0, strrpos($_SERVER["SCRIPT_FILENAME"], "/ca")));
    }

    $content .= '<table>
        <tr>
            <td colspan="2">PHP Info</td>
        </tr>
        <tr>
            <td style="width: 30%;">Safe Mode:</td>
            <td>' . onOff(ini_get("safe_mode")) . '</td>
        </tr>
        <tr>
            <td>Display Errors:</td>
            <td>' . onOff(ini_get("display_errors")) . '</td>
        </tr>
        <tr>
            <td>Ignore Repeated Errors:</td>
            <td>' . onOff(ini_get("ignore_repeated_errors")) . '</td>
        </tr>
        <tr>
            <td>Register Globals:</td>
            <td>' . onOff(ini_get("register_globals")) . '</td>
        </tr>
        <tr>
            <td>Disabled Functions:</td>
            <td>' . ini_get("disable_functions") . '</td>
        </tr>
        <tr>
            <td>Allow URL fopen:</td>
            <td>' . onOff(ini_get("allow_url_fopen")) . '</td>
        </tr>
        <tr>
            <td>Max Execution Time:</td>
            <td>' . ini_get("max_execution_time") . '</td>
        </tr>
        <tr>
            <td>Post Max Size:</td>
            <td>' . ini_get("post_max_size") . '</td>
        </tr>
    </table>

<form action="install.php" method="post" class="well">
<input type="hidden" name="step" value="3" />
<input type="hidden" name="function_name" value="paths" />
<table>
    <tr>
        <td colspan="2">' . tf('setPaths', 'administrator') . '</td>
    </tr>
    <tr>
        <td style="width: 30%;">' . tf('urlAddress', 'administrator') . ':</td>
        <td>
<input type="text" name="sitelink" value="http://' . $_SERVER["SERVER_NAME"] . $additional . '" /></td>
    </tr>
    <tr>
        <td>' . tf('pathToDirectory', 'administrator') . ':</td>
        <td><input type="text" name="basedir" value="' . substr($_SERVER["SCRIPT_FILENAME"], 0, strrpos($_SERVER["SCRIPT_FILENAME"], "/ca")) . '" /></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" value="' . tf('continueInstallation', 'administrator') . '" class="btn btn-primary" /></td>
    </tr>
</table>
</form>';
} elseif ($step == 3) {
    if ($_POST["function_name"] == 'paths') {

        $basedir = $_POST["basedir"];
        $sitelink = $_POST["sitelink"];

        cperms($basedir . '/index.php');
        cperms($basedir . '/sitemap.xml');
        cperms($basedir . '/cal-bin/contacts');
        cperms($basedir . '/cal-bin/pages');
        cperms($basedir . '/cal-components/cache');
        cperms($basedir . '/cal-components/classes');
        cperms($basedir . '/cal-components/data');
        cperms($basedir . '/cal-components/data-public');
        cperms($basedir . '/cal-components/data/modules.xml');
        cperms($basedir . '/cal-components/data/extensions.xml');
        cperms($basedir . '/cal-components/data/templates.xml');
        cperms($basedir . '/cal-components/data/blacklist.xml');
        cperms($basedir . '/cal-components/data/categories.xml');
        cperms($basedir . '/cal-components/data/contacts.xml');
        cperms($basedir . '/cal-components/data/lang.xml');
        cperms($basedir . '/cal-components/data/pages.xml');
        cperms($basedir . '/cal-components/data/permissions.xml');
        cperms($basedir . '/cal-components/data/settings.xml');
        cperms($basedir . '/cal-components/data/users.xml');
        cperms($basedir . '/cal-components/data-public/countries.xml');
        cperms($basedir . '/cal-components/extensions');
        cperms($basedir . '/cal-components/modules');
        cperms($basedir . '/cal-pages');
        cperms($basedir . '/cal-pages/index.html');
        cperms($basedir . '/cal-pages/kontakt.html');
        cperms($basedir . '/cal-pages/p404.html');
        cperms($basedir . '/cal-templates');
        cperms($basedir . '/cal-templates/caloris-admin');
        cperms($basedir . '/cal-templates/caloris-basic');
        cperms($basedir . '/cal-templates/caloris-admin/basic.xml');
        cperms($basedir . '/cal-templates/caloris-basic/basic.xml');
        cperms($basedir . '/cal-templates/caloris-basic/css/style.css');
        cperms($basedir . '/cal-bin/pages');
        cperms($basedir . '/cal-bin/contacts');

        $setfile = $basedir . '/cal-components/data/settings.xml';

        $xma = simplexml_load_file($setfile);

        $xma->paths->base = $basedir;
        $xma->paths->uri = $sitelink;
        $xma->paths->console = $sitelink . '/cal-core/console.php';
        $xma->asXML($setfile);
    }

    $content .= '
<form action="install.php" method="post" class="well">
<input type="hidden" name="step" value="4" />
<input type="hidden" name="function_name" value="all" />
<input type="hidden" name="basedir" value="' . $basedir . '" />
<input type="hidden" name="sitelink" value="' . $sitelink . '" />

<h2>' . tf('basicSettings', 'administrator') . '</h2>

<table>
    <tr>
        <td style="width: 30%;">' . tf('siteName', 'administrator') . ':</td>
        <td><input type="text" name="sitetitle" value="' . $_GET["sitetitle"] . '" /></td>
    </tr>
    <tr>
        <td>' . tf('siteCode', 'administrator') . ':</td>
        <td><input type="text" name="sitecode" value="' . $_GET["sitecode"] . '" /></td>
    </tr>
    <tr>
        <td>' . tf('administratorEmail', 'administrator') . ':</td>
        <td><input type="text" name="siteemail" value="' . $_GET["siteemail"] . '" /></td>
    </tr>
</table>

<h2>' . tf('setDatabase', 'administrator') . ' (' . tf('optional', 'administrator') . ')</h2>

<table>
    <tr class="small">
        <td>' . tf('databaseType', 'administrator') . ':</td>
        <td>
<select name="dbtype">
	<option value="mysql" selected="selected">MySQL</option>
	<option value="pgsql">PostgreSQL</option>
</select>
        </td>
    </tr>
    <tr>
        <td>' . tf('databaseAddress', 'administrator') . ':</td>
        <td><input type="text" name="dbaddress" value="' . $_GET["dbaddress"] . '" /></td>
    </tr>
    <tr class="small">
        <td>' . tf('databaseName', 'administrator') . ':</td>
        <td><input type="text" name="dbname" value="' . $_GET["dbname"] . '" /></td>
    </tr>
    <tr class="small">
        <td>' . tf('databaseUserName', 'administrator') . ':</td>
        <td><input type="text" name="dbuser" value="' . $_GET["dbuser"] . '" /></td>
    </tr>
    <tr class="small">
        <td>' . tf('databasePassword', 'administrator') . ':</td>
        <td><input type="password" name="dbpass" /></td>
    </tr>
    <tr class="small">
        <td>&nbsp;</td>
        <td><input type="submit" value="' . tf('continueInstallation', 'administrator') . '" class="btn btn-primary" /></td>
    </tr>
</table>
</form>' . PHP_EOL;
} elseif ($step == 4) {
    if ($_POST["function_name"] == 'all') {
        $basedir = $_POST["basedir"];
        $sitetitle = $_POST["sitetitle"];
        $sitelink = $_POST["sitelink"];
        $sitecode = $_POST["sitecode"];
        $siteEmail = $_POST["siteemail"];
        $dbtype = $_POST["dbtype"];
        $dbaddress = $_POST["dbaddress"];
        $dbname = $_POST["dbname"];
        $dbuser = $_POST["dbuser"];
        $dbpass = $_POST["dbpass"];

        $setfile = $basedir . '/cal-components/data/settings.xml';
        $xma = simplexml_load_file($setfile);
        $xma->basic->admin_email = $siteEmail;
        $xma->basic->state = 'on';
        $xma->basic->title = $sitetitle;
        $xma->basic->title_short = $sitecode;
        $xma->database->user = $dbuser;
        $xma->database->type = $dbtype;
        $xma->database->name = $dbname;
        $xma->database->password = $dbpass;
        $xma->database->address = $dbaddress;
        $xma->database->port = 5432;
        $xma->asXML($setfile);

        // Set admin e-mail in users.xml
        $xmlUser = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/users.xml');
        $xmlUserEdit = $xmlUser->xpath('//user[@id="admin"]');
        $xmlUserEdit[0]->email = $siteEmail;
        $xmlUser->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/users.xml');

        // HTACCESS
        if (file_exists($basedir . '/htaccess.txt')) {
            rename($basedir . '/htaccess.txt', $basedir . '/.htaccess');
        }

        header("Location: install.php?step=5");
        exit();
    }
}

if ($_GET["step"] == 5) {
    $content .= '
<p class="perex">' . tf('installationSuccessful', 'administrator', '<a href="' . _CALSET_PATHS_URI . '/administrator">') . '</a>. 
<p class="perex">' . tf('yourUserName', 'administrator', '<span style="font-style: italic;">admin</span>') . ' 
 ' . tf('yourPassword', 'administrator', '<span style="font-style: italic;">cal-caloris</span>') . ' 
' . tf('yourPasswordChange', 'administrator', '<span style="font-style: italic;">' . tf('settings', 'administrator') . '</span>') . '</p>

<p>' . tf('nowYouCan', 'administrator') . ' <a href="login.php">' . tf('login', 'administrator') . '</a></p>

<br />
<br />
<br />
';
} elseif ($_GET["step"] > 5 or $_REQUEST["step"] == '') {

    $content .= '
<p class="perex" style="width: 80%; text-align: center;">' . tf('welcomeText', 'administrator') . '</p>

<form action="install.php" method="post" class="well">
<input type="hidden" name="step" value="2" />
<iframe src="../../../LICENSE.txt" style="width: 100%; height: 25em; min-width: 320px;"></iframe>
<p><input type="checkbox" name="agree" value="1" />&nbsp;' . tf('agreeWithAgreement', 'administrator') . '</p>
<p><input type="submit" value="' . tf('continueInstallation', 'administrator') . '" class="btn btn-primary" /></p>
</form>';
}

echo '
<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Caloris: ' . tf('installation', 'administrator') . ': ' . tf('stepNr', 'administrator') . '' . $step . '</title>
    <link rel="stylesheet" href="../../../cal-templates/caloris-admin/bootstrap.min.css" type="text/css" />
</head>
<body style="margin: 0 20px 0 20px;">';

echo '
<table cellspacing="0" cellpadding="0" style="background-color: #646464; color: white; width: 100%; height: 22px;">
	<tr>
		<td style="vertical-align: middle; padding-left: 10px;">' . _CALSET_CALORIS_NAME . ' ' . _CALSET_CALORIS_VERSION . '</td>
		<td style="text-align: right; padding-right: 10px; vertical-align: middle;"></td>
	</tr>
</table>

<div style="background-image: url(../cal-templates/caloris-admin/images/imgmin-banner2.jpg); background-repeat: repeat-x; height: 59px; width: 100%; margin-bottom: 20px;">
<div style="background-image: url(../cal-templates/caloris-admin/images/imgmin-banner1.jpg); background-repeat: no-repeat; height: 59px; width: 100%;">

<p style="font-size: 25px; font-family: monospace; padding: 15px 0 30px 25px;">' . tf('installer', 'administrator') . '</p></td>
</div>
</div>';

echo '

<div style="padding: 0 50px;">
<h1>' . tf('stepNr', 'administrator') . '' . $step . '</h1>';

echo $content . '
</div>    
<div class="footer" style="margin: 20px 0 0 0;"><p><a href="http://caloris.cz">Caloris</a> is Free Software released under the <a href="http://www.gnu.org/licenses/gpl-3.0.html">GNU/GPL License</a>.</p></div></div>
</body>
</html>
';

function onOff($val) {
    if ($val == 1) {
        $x = 'On';
    } else {
        $x = 'Off';
    }

    return $x;
}

function cperms($file) {
    error_reporting(E_NOTHING);
    ini_set('display_errors', 'Off');

    if (substr(decoct(fileperms($file)), 2) != '0777') {
        if (chmod($file, 0777) == false) {
            echo tf('setPermissionsFail', 'administrator') . ': <em>' . $file . '</em>. ' . tf('setPermissionsFailManual', 'administrator') . '<br />';
            exit();
        }
    }

    error_reporting(E_ALL ^ E_NOTICE);
    ini_set('display_errors', 'On');
}

// Funkce pro načítání XML nastavení do PHP konstant
function loadConstants($file, $define = '_CALSET_') {
    $xml = simplexml_load_file($file);

    foreach ($xml->children() as $child) {

        if (count($child->children()) == 0) {
            define($define . strtoupper($child->getName()), $child);
        }

        foreach ($child->children() as $childofchild) {

            if (count($childofchild->children()) > 0) {
                foreach ($childofchild->children() as $childofchildofchild) {
                    define($define . strtoupper($child->getName()) . "_" . strtoupper($childofchild->getName()) . "_" . strtoupper($childofchildofchild->getName()), $childofchildofchild);
                }
            } else {
                define($define . strtoupper($child->getName()) . "_" . strtoupper($childofchild->getName()), $childofchild);
            }
        }
    }
}

/* tf: Translating extension */

function tf($keyword, $source = '', $replace = '') {
    $lang = 'cs';

    if ($source == '') {
        $xmlPath = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/lang.xml';
    } else {
        $xmlPath = 'lang.xml';
    }

    $xmlLanguageF = simplexml_load_file($xmlPath);

    $xmlLanguage = $xmlLanguageF->xpath('//snippets[@id="' . $keyword . '"]/' . $lang);

    if ($replace != '') {
        $content = str_replace('%T', $replace, $xmlLanguage[0]);
    } else {
        $content = $xmlLanguage[0];
    }

    return $content;
}